package xyz.syyrjx.consumer.load;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import xyz.syyrjx.consumer.client.SyyrjxRpcConsumerClient;
import xyz.syyrjx.consumer.client.exception.SyyrjxRpcConsumerException;
import xyz.syyrjx.consumer.conf.SyyrjxRpcConsumerConfig;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2023/1/9 - 21:12
 */
public class SyyrjxRpcConsumerLoadAware implements ApplicationContextAware {

    private final static Logger LOGGER = LoggerFactory.getLogger(SyyrjxRpcConsumerLoadAware.class);

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SyyrjxRpcConsumerClient consumerClient = applicationContext.getBean(SyyrjxRpcConsumerClient.class);
        try {
            while (consumerClient.getClient().getChannel() == null || !consumerClient.getClient().getChannel().isActive()) {
                consumerClient.start();
            }
        } catch (InterruptedException e) {
            throw new SyyrjxRpcConsumerException(SyyrjxRpcConsumerException.CONSUMER_CLIENT_START_ERROR);
        }
    }
}
